﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="TemplateSelector.cs" company="Marcin Jastrzebski">
//   Copyright (c) Marcin Jastrzebski (marcin@marcinj.net)
// </copyright>
// <summary>
//   Selects the right template based on the item type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------
namespace mPassRT.Selectors
{
    using System.Collections.Generic;

    using Windows.UI.Xaml;
    using Windows.UI.Xaml.Controls;

    /// <summary>Selects the right template based on the item type.</summary>
    public class TemplateSelector : DataTemplateSelector
    {
        #region Constructors and Destructors

        /// <summary>Initializes a new instance of the <see cref="TemplateSelector"/> class.</summary>
        public TemplateSelector()
        {
            this.Mappings = new List<TemplateSelectorMapping>();
        }

        #endregion

        #region Public Properties

        /// <summary>Gets or sets the mappings.</summary>
        public List<TemplateSelectorMapping> Mappings { get; set; }

        #endregion

        #region Methods

        /// <summary>Selects the template.</summary>
        /// <param name="item">The item.</param>
        /// <param name="container">The container.</param>
        /// <returns>The <see cref="DataTemplate"/>.</returns>
        protected override DataTemplate SelectTemplateCore(object item, DependencyObject container)
        {
            if (item == null)
            {
                return null;
            }

            var mapping = (TemplateSelectorMapping)SelectorHelper.GetMapping(item, this.Mappings);
            return mapping == null ? null : mapping.Template;
        }

        #endregion
    }
}